package dilmaj.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dilmaj.shared.StarEntryVO;
import dilmaj.shared.StarSuggestionVO;
import dilmaj.shared.StarTermVO;

import java.util.*;

public class StarTablePanel extends PopupPanel {
	private DilmajConstants constants = GWT.create(DilmajConstants.class);

	private StarEntryVO baseEntryVO=null;
	
	private Object[] termsFound;
	private Object[] sourcesFound;
	private Object[] languagesFound;
	private Object[] areasFound;
	private Object[] suggestionsFound;
	private Object[] commentsFound;
	private Object[] usecasesFound;
	
	private FlexTable entriesTable=new FlexTable();
	
	private HorizontalPanel mainPanel=new HorizontalPanel();
	private HorizontalPanel newTermPanel=new HorizontalPanel();
	private VerticalPanel tablePanel=new VerticalPanel();
	
	private TextBox inputBox=new TextBox();
	private Button inputButton=new Button(constants.create());
	
	private TextBox filterBox=new TextBox();
	private Button searchButton=new Button(constants.search());
	private ListBox filterList=new ListBox(true);

	private TextArea descriptionBox=new TextArea();
	
	private boolean isPopup;
	private String entryType;
	
	private Button closeButton=new Button(constants.close());
	
	private StarPanel basePanel;
	
	private String fullFilter="";

	public StarTablePanel(String[] inputFilter, String entryType, boolean isPopup, StarEntryVO defaultEntryVO, StarPanel basePanel) {
		baseEntryVO=defaultEntryVO;
		this.basePanel=basePanel;
		
		this.isPopup=isPopup;
		this.entryType=entryType;

		newTermPanel.add(inputBox);
		newTermPanel.add(inputButton);
		refreshLogin();

		if (isPopup) {
			newTermPanel.add(closeButton);
			closeButton.addClickHandler(new SearchCloseHandler(baseEntryVO, this, basePanel));
		}
		searchButton.addClickHandler(new SearchStarHandler(filterBox, filterList, this));
		filterList.addItem(constants.term());
		filterList.addItem(constants.suggestion());
		filterList.addItem(constants.area());
		filterList.addItem(constants.usecase());
		filterList.addItem(constants.comment());
		filterList.addItem(constants.source());
		filterList.addItem(constants.language());
		filterList.addItem(constants.all());
		
		if (inputFilter.length>0) {
			filterList.setEnabled(false);
			for (int i=0;i<inputFilter.length;i++) {
				for (int j=0;j<filterList.getItemCount();j++) {
					String item=filterList.getItemText(j);
					String filter=inputFilter[i];
					if (item.compareToIgnoreCase(filter)==0) {
						fullFilter=fullFilter.concat(filter);
						filterList.setItemSelected(j, true);
					}
				}
			}
		} else 
			filterList.setItemSelected(0, true);

		ScrollPanel termsPanel=new ScrollPanel();
		termsPanel.add(entriesTable);
		tablePanel.add(termsPanel);

		if (fullFilter.contains(constants.comment()) || fullFilter.contains(constants.usecase())) {
			newTermPanel.add(descriptionBox);
			termsPanel.setSize("400px", "200px");
		} else  {
			newTermPanel.add(filterBox);
			newTermPanel.add(searchButton);
			newTermPanel.add(filterList);
			termsPanel.setSize("200px", "400px");
		}
		
		mainPanel.add(tablePanel);
		mainPanel.add(newTermPanel);
		
		inputButton.addClickHandler(new AddStarHandler(inputBox, entryType, this));
		
		add(mainPanel);
		
		if (baseEntryVO!=null) {
			if (fullFilter.contains(constants.comment())) {
				DataStore.setFoundComments(baseEntryVO.getAttachmentsOfType(constants.comment()).toArray());
				populateTable(constants.comment());
			}
			if (fullFilter.contains(constants.usecase())) {
				DataStore.setFoundUseCases(baseEntryVO.getAttachmentsOfType(constants.usecase()).toArray());
				populateTable(constants.usecase());
			}
		}
	}

	public void refreshLogin() {
		if (DataStore.getUserid()!=-1) {
			inputBox.setVisible(true);
			inputButton.setVisible(true);
		} else if (entryType.compareToIgnoreCase(constants.comment())==0) {
			inputBox.setVisible(true);
			inputButton.setVisible(true);
		} else {
			inputBox.setVisible(false);
			inputButton.setVisible(false);
		}
	}
	
	public String getDataDescription() {
		String test=descriptionBox.getText();
		return test;
	}
	
	public void populateTable(String filter) {
		entriesTable.removeAllRows();

		int row=0;
		
		sourcesFound=DataStore.getFoundSources();
		if (filter.contains(constants.source()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, constants.sources());
			for (int i=0;i<sourcesFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)sourcesFound[i], isPopup));
			}
		}
		languagesFound=DataStore.getFoundLanguages();
		if (filter.contains(constants.language()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, "languages");
			for (int i=0;i<languagesFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)languagesFound[i], isPopup));
			}
		}
		termsFound=DataStore.getFoundTerms();
		if (filter.contains(constants.term()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, "terms");
			for (int i=0;i<termsFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)termsFound[i], isPopup));
			}
		}
		suggestionsFound=DataStore.getFoundSuggestions();
		if (filter.contains(constants.suggestion()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, "suggestions");
			for (int i=0;i<suggestionsFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)suggestionsFound[i], isPopup));
			}
		}
		areasFound=DataStore.getFoundAreas();
		if (filter.contains(constants.area()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, constants.areas());
			for (int i=0;i<areasFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)areasFound[i], isPopup));
			}
		}
		commentsFound=DataStore.getFoundComments();
		if (filter.contains(constants.comment()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, constants.comments());
			for (int i=0;i<commentsFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)commentsFound[i], isPopup));
			}
		}
		usecasesFound=DataStore.getFoundUseCases();
		if (filter.contains(constants.usecase()) || filter.contains(constants.all())) {
			entriesTable.setText(row++, 0, constants.usecase());
			for (int i=0;i<usecasesFound.length;i++) {
				entriesTable.setWidget(row++, 0, new StarButtonPanel(baseEntryVO, (StarEntryVO)usecasesFound[i], isPopup));
			}
		}
	}
	
	public void addRow(StarEntryVO newEntry) {
		//int row=entriesTable.getRowCount();
		entriesTable.insertRow(0);

		if (entryType.compareToIgnoreCase(constants.area())==0)
			entriesTable.setText(0, 0, constants.areas());
		if (entryType.compareToIgnoreCase(constants.source())==0)
			entriesTable.setText(0, 0, constants.sources());
		if (entryType.compareToIgnoreCase(constants.language())==0)
			entriesTable.setText(0, 0, constants.languages());
		if (entryType.compareToIgnoreCase(constants.comment())==0)
			entriesTable.setText(0, 0, constants.comments());
		if (entryType.compareToIgnoreCase(constants.usecase())==0)
			entriesTable.setText(0, 0, constants.usecase());
		if (entryType.compareToIgnoreCase(constants.term())==0)
			entriesTable.setText(0, 0, constants.terms());
		
		entriesTable.setWidget(1, 0, new StarButtonPanel(baseEntryVO, newEntry, isPopup));
	}
	
	public List<StarEntryVO> getSelectedVOs() {
		List<StarEntryVO> selectedVOs=new ArrayList<StarEntryVO>();
		
		for (int i=0;i<entriesTable.getRowCount();i++) {
			StarButtonPanel currentPanel=(StarButtonPanel)entriesTable.getWidget(i, 0);
			if (currentPanel!=null) {
				if (currentPanel.isChecked()) {
					selectedVOs.add(currentPanel.getData());
				}
			}
		}
		
		return selectedVOs;
	}
	
	public void clearTable() {
		entriesTable.removeAllRows();
	}
}
